# coding=utf-8
import time
from threading import Thread
import os
import sys
import nvm
import signal
import client

# pid = 0
ppp_test = 1
rndis_test = 2
cmux_test = 3
test_mode = 0
LocIfList = []
test_object = None
execfile("../cfg/at/other.cfg")


# def creat_diag_print():
#     nowTime = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
#     os.system("./../bin/diag "+" ../log/"+log_dir+'/'+nowTime)


def output_result():
    for i in range(len(LocIfList)):
        os.system("ifconfig "+LocIfList[i]+" up")
        print "ifconfig "+LocIfList[i]+" up"

    print 'ppp拨号测试总共进行了'+(test_object.getv('ppp_test_time'))+'次，成功了'+(
        test_object.getv('ppp_test_suc'))+'次，失败了'+(test_object.getv('ppp_test_fail'))+'次'
    print 'ppp拨号测试总共ping了'+(test_object.getv('ppp_ping_time'))+'次，成功了'+(
        test_object.getv('ppp_ping_suc'))+'次，失败了'+(test_object.getv('ppp_ping_fail'))+'次'
    print 'ppp出现了unknow_host'+test_object.getv('ppp_unknow_host')+'次'
    print 'RNDIS网卡测试总共进行了'+(test_object.getv('rndis_test_time'))+'次，成功了'+test_object.getv(
        "rndis_test_suc")+'次，失败了'+test_object.getv("rndis_test_fail")+'次'
    print 'RNDIS网卡测试中总共ping了'+test_object.getv("rndis_ping_time")+'次，成功了'+test_object.getv(
        "rndis_ping_suc")+'次，失败了'+test_object.getv("rndis_ping_fail")+'次'
    print 'RNIDS出现了unknow_host'+test_object.getv('ppp_unknow_host')+'次'
    print 'cmux拨号测试总共进行了'+test_object.getv("cmux_test_time")+'次，成功了'+test_object.getv(
        "cmux_test_suc")+'次，失败了'+test_object.getv("cmux_test_fail")+'次'
    print 'cmux拨号测试中总共ping了'+test_object.getv("cmux_ping_time")+'次，成功了'+test_object.getv(
        "cmux_ping_suc")+'次，失败了'+test_object.getv("cmux_ping_fail")+'次'
    print 'cmux出现了unknow_host'+test_object.getv('ppp_unknow_host')+'次'


'''def diag_exit():
    # os.system("kill -9 $(pidof diag)")
    os.kill(pid, signal.SIGINT)
    try:
        os.waitpid(pid, 0)
    except:
        exit(5)'''


class Test(nvm.Nvm, object):
    def __init__(self, path, test_mode):
        super(Test, self).__init__(path)
        self.mode = test_mode
        # self.process_diag = Thread(target=creat_diag_print, name='diag_thread')

    # def test_start(self):
        # self.process_diag.start()

    def test_exit(self):
        output_result()
        # diag_exit()

    def get_test_mode(self):
        if self.mode == 1:
            return 'ppp'
        elif self.mode == 2:
            return 'rndis'
        elif self.mode == 3:
            return 'cmux'

    def add_ping_fail(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_ping_fail', num)
            client.add('ppp_ping_fail', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_ping_fail', num)
            client.add('rndis_ping_fail', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_ping_fail', num)
            client.add('cmux_ping_fail', num)

    def add_ping(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_ping_time', num)
            client.add('ppp_ping_time', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_ping_time', num)
            client.add('rndis_ping_time', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_ping_time', num)
            client.add('cmux_ping_time', num)

    def add_ping_suc(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_ping_suc', num)
            client.add('ppp_ping_suc', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_ping_suc', num)
            client.add('rndis_ping_suc', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_ping_suc', num)
            client.add('cmux_ping_suc', num)

    def add_test_time(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_test_time', num)
            client.add('ppp_test_time', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_test_time', num)
            client.add('rndis_test_time', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_test_time', num)
            client.add('cmux_test_time', num)

    def add_suc_time(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_test_suc', num)
            client.add('ppp_test_suc', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_test_suc', num)
            client.add('rndis_test_suc', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_test_suc', num)
            client.add('cmux_test_suc', num)

    def add_fail_time(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_test_fail', num)
            client.add('ppp_test_fail', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_test_fail', num)
            client.add('rndis_test_fail', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_test_fail', num)
            client.add('cmux_test_fail', num)

    def add_unknow_host(self, num):
        if self.mode == 1:  # ppp拨号ping测试
            self.addv('ppp_unknow_host', num)
            client.add('ppp_unknow_host', num)
        elif self.mode == 2:  # rndis ping测试
            self.addv('rndis_unknow_host', num)
            client.add('rndis_unknow_host', num)
        elif self.mode == 3:  # cmux ppp拨号测试
            self.addv('cmux_unknow_host', num)
            client.add('cmux_unknow_host', num)
